<?php

namespace Home\Controller;

use Think\Controller;
use Think\Db;

class IndexController extends Controller
{
    public function index()
    {
    }

    public function get_department_list()
    {
        $res = SelectInDB('Department');
        make_json_result(1, "success", $res);
    }

    public function del_department()
    {
        $request = TakeData(['id']);
        $res = DeleteInDB('Department', ['id' => $request['id']]);
        if($res){
            make_json_result(1, "success", []);
        }else{
            make_json_result(0, "fail", []);
        }
    }

    public function get_student_info()
    {
        $request = TakeData(['study_number'], true);
        $where['study_number'] = $request['study_number'];
        $result = SelectInDB("Form", $where);
        make_json_result(1, "success", $result);
    }

    public function set_student_info()
    {
        $request = TakeData(['study_number'], true);
        $post = $_POST ? $_POST : $_GET;
        unset($post['study_number']);
        if (($_POST['chushengnianyue']) && ($_POST['shenqingshijian'])) {
            if (strtotime($_POST['chushengnianyue']) > (strtotime($_POST['shenqingshijian']) - (3600 * 24 * 365 * 18))) {
                echo "<script>alert('申请时间必须大于18周岁');</script>";
                exit;
            }
        }

        if (($_POST['shenqingshijian']) && ($_POST['jijifenzigongshishijian'])) {
            if (strtotime($_POST['shenqingshijian']) > (strtotime($_POST['jijifenzigongshishijian']) - (3600 * 24 * 180))) {
                echo "<script>alert('确定为积极分子时间要大于申请入党时间的6个月');</script>";
                exit;
            }
        }
        if (($_POST['jijifenzigongshishijian']) && ($_POST['zhengshengongshishijian'])) {
            if (strtotime($_POST['jijifenzigongshishijian']) > (strtotime($_POST['zhengshengongshishijian']) - (3600 * 24 * 365))) {
                echo "<script>alert('确定为发展对象的时间必须是确定为积极分子一年后');</script>";
                exit;
            }
        }
        $DB = M('Form');
        $DB->startTrans();
//        $sql = "DELETE FROM dy_form WHERE study_number = '{$request["study_number"]}'";
//        $DB->execute($sql);
        $insert_data = [];
        foreach ($post as $key => $value) {
            if ($value != "") {
                $data['key'] = $key;
                $data['value'] = $value;
                $data['study_number'] = $request['study_number'];
                $insert_data[] = $data;
            }
        }
        $result = $DB->addAll($insert_data, [], true);
        if (!$result) {
            $DB->rollback();
            echo "<script>alert('fail');</script>";
            exit;
        }
        $DB->commit();
        echo "<script>alert('success');</script>";
        exit;
    }

    public function add_student()
    {
        $request = TakeData(['department', 'study_number', 'name'], true);
        if (AddInDB('Student', $request)) {
            echo "<script>alert('success');</script>";
        } else {
            echo "<script>alert('fail');</script>";
        }
    }

    public function add_department()
    {
        $request = TakeData(['department'], true);
        if (AddInDB('Department', $request)) {
            make_json_result(1, "success", []);
        } else {
            make_json_result(0, "fail", []);
        }
    }

    public function add_admin()
    {
        $request = TakeData(['uname', 'pwd', 'department'], true);
        $request['type'] = 2;
        if (AddInDB('User', $request)) {
            make_json_result(1, "success", []);
        } else {
            make_json_result(0, "fail", []);
        }
    }

    public function get_student_list()
    {
        $request = TakeData(['department'], true);
        if ($request['department'] == '0') {
            unset($request['department']);
        }
        $result = SelectInDB("Student", $request);
        make_json_result(1, "success", $result);
    }

    public function login()
    {
        $request = TakeData(['uname', 'pwd'], true);
        $result = FindInDB('User', $request);
        if ($result) {
            make_json_result(1, 'success', $result);
        } else {
            make_json_result(0, 'fail', []);
        }
    }
}